package com.pucheng.bracelet.ui.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
import com.blankj.utilcode.util.LogUtils
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.util.click
import kotlinx.android.synthetic.main.dialog_time_select.*
import kotlin.math.min

class TimeSelectDialog(mContext: Context, val time: String) :
    Dialog(mContext, R.style.DialogStyle) {

    private var ampmIndex: Int = 0
    private var hour: Int = 1
    private var minute: String = ""
    var timeSelectCallBack: TimeSelectCallBack? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_time_select)
        val times = time.split("[:]".toRegex())
        hour = times[0].toInt()
        if (hour > 12 || hour == 0) {
            if (hour == 0) {
                hour = 12
            } else {
                hour -= 12
            }
            ampmIndex = 1
        } else {
            ampmIndex = 0
        }
        minute = times[1]


        tv_cancel.click {
            dismiss()
        }
        tv_done.click {
            dismiss()
            if (timeSelectCallBack != null) {
                if (ampmIndex == 1) {
                    if (hour != 12) hour += 12 else hour = 0
                }
                timeSelectCallBack!!.onSelectTime("${if (hour>10){hour}else{
                    "0$hour"
                }}:$minute")
            }
        }
        wv_ampm.setCyclic(false)
        wv_ampm.setOnItemSelectedListener { index ->
            LogUtils.e("index:$index")
            ampmIndex = index
        }

        wv_hour.setOnItemSelectedListener { index ->
            LogUtils.e("index:$index")
            hour = index + 1
//                mViewModel.changeTime(apIndex = index)
        }
        wv_minute.setOnItemSelectedListener { index ->
            LogUtils.e("index:$index")
            if (index>9) {
                minute = index.toString()
            }else{
                minute = "0$index"
            }
//                mViewModel.changeTime(apIndex = index)
        }

        val window = window
        window!!.setGravity(Gravity.BOTTOM)
        val attributes = window.attributes
        window.attributes = attributes
        setCanceledOnTouchOutside(true)

        initWheel()
    }

    private fun initWheel() {
        val itemsAP: MutableList<String> = mutableListOf<String>().apply {
            add("上午")
            add("下午")
        }
        wv_ampm.adapter = ArrayWheelAdapter(itemsAP)

        val itemsHour: MutableList<String> = mutableListOf<String>().apply {
            for (i in 1..12) {
                add("$i")
            }
        }
        wv_hour.adapter = ArrayWheelAdapter(itemsHour)

        val itemsMinute: MutableList<String> = mutableListOf<String>().apply {
            for (i in 0..59) {
                if (i < 10) {
                    add("0$i")
                } else {
                    add("$i")
                }
            }
        }
        wv_minute.adapter = ArrayWheelAdapter(itemsMinute)

        wv_ampm.currentItem = ampmIndex
        wv_hour.currentItem = hour - 1
        wv_minute.currentItem = minute.toInt()
    }

    fun setOnTimeSelectCallBack(timeSelectCallBack: TimeSelectCallBack): TimeSelectDialog {
        this@TimeSelectDialog.timeSelectCallBack = timeSelectCallBack
        return this@TimeSelectDialog
    }

    interface TimeSelectCallBack {
        fun onSelectTime(time: String?)
    }

}